﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project_KhuyenMaiVang.DAO;
using Project_KhuyenMaiVang.Models;

namespace Project_KhuyenMaiVang.Controllers
{
    public class MemberController : Controller
    {
        //
        // GET: /Member/

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult CapNhatThongTinCaNhan()
        {
            AccountDAO accountDAO = new AccountDAO();
            int idAccount = accountDAO.GetIDAccountFrom(User.Identity.Name);
            MemberDAO memberDAO = new MemberDAO();
            MemberDTO memberDTO = memberDAO.GetInfor(idAccount);
            MemberInfo memberInfo = new MemberInfo("", "", "", "","","");
            if(memberDTO != null)
                memberInfo = new MemberInfo(memberDTO.TenKhachHang, memberDTO.DiaChi, memberDTO.TaiKhoanPayPal, "","","");
            return View(memberInfo);
        }

        [HttpPost]
        public ActionResult CapNhatThongTinCaNhan(MemberInfo memberInfo)
        {
            MemberDAO memberDAO = new MemberDAO();
            AccountDAO accountDAO = new AccountDAO();
            int idAccount = accountDAO.GetIDAccountFrom(User.Identity.Name);
            memberDAO.UpdateMember(new MemberDTO(idAccount, memberInfo.TenKhachHang, memberInfo.DiaChi, "", memberInfo.TaiKhoanPayPal));
            if(memberInfo.CurrentPassWord != null && memberInfo.NewPassword1 == memberInfo.NewPassword2)
                accountDAO.updateAccountInfo(idAccount, memberInfo.NewPassword1);
            return RedirectToAction("CapNhatThongTinCaNhan", "Member");
        }

        public ActionResult LichSuMuaHang()
        {
            DonDatHangDAO donDatHangDAO = new DonDatHangDAO();
            //List<DonDatHangDTO> dsDonDatHangDTO = donDatHangDAO.getAllOrders();
            AccountDAO accountDAO = new AccountDAO();
            int idAccount = accountDAO.GetIDAccountFrom(User.Identity.Name);
            List<DonDatHangDTO> dsDonDatHangDTO = donDatHangDAO.getAllOrdersBoughtBy(idAccount);
            List<DonDatHang> dsDonDatHang = new List<DonDatHang>();
            for (int i = 0; i < dsDonDatHangDTO.Count; i++)
            {
                DonDatHang temp = new DonDatHang();
                temp.MaDonDatHang = dsDonDatHangDTO[i].MaDonDatHang;
                temp.NguoiDatHang = (new AccountDAO()).findWithIDAccount(dsDonDatHangDTO[i].IDAccount).UserName;
                temp.TenSanPham = (new TinKhuyenMaiDAO()).getDetailOfSaleOffNews(dsDonDatHangDTO[i].MaTinKhuyenMai).ThongTinTomTat;
                temp.SoLuong = dsDonDatHangDTO[i].SoLuong;
                temp.TinhTrangDonHang = (new TinhTrangDonDatHangDAO()).getOrderState(dsDonDatHangDTO[i].MaTinhTrangDonDatHang);
                dsDonDatHang.Add(temp);
            }
            return View(dsDonDatHang);

        }

        public ActionResult TinhTrangDonDatHang()
        {
            return View();
        }
    }
}
